﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace search
{
	/// <summary>
	/// EditArticle 的摘要说明。
	/// </summary>
	public partial class Title : System.Web.UI.Page
	{
		private DataSet dsTopics;

	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			HttpCookie nameCookie = Request.Cookies["NickName"];
			if(nameCookie == null)
				Response.Redirect("../client/index.aspx");

			if(!Page.IsPostBack)
			{
				this.Button_DeleteTitle.Attributes["onclick"] = "return confirm('确实要删除吗？');";
				this.Button_DeleteSubTitle.Attributes["onclick"] = "return confirm('确实要删除吗？');";
				BindListBox();
				BindListBoxSub();
			}
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion


		private void BindListBox()
		{
			dsTopics = new DataSet();
//			string filename = Server.MapPath(".") + "\\topics.xml";
//			dsTopics.ReadXml(filename);
//
//			this.ListBox_Title.DataSource = dsTopics.Tables["topics"].DefaultView;
//			this.ListBox_Title.DataTextField = "mainname";
//			this.ListBox_Title.DataValueField = "mainname";
//			this.ListBox_Title.DataBind();
//			
//			this.ListBox_Title.SelectedIndex = 0;
//			this.TextBox_Title.Text = this.ListBox_Title.SelectedValue;

			dsTopics = SearchDataAccess.GetTopic();
			if(dsTopics.Tables[0].Rows.Count == 0)
			{
				SearchDataAccess.InsertTopic("互教", "耶稣的神性,上帝的存在,耶稣的复活,圣经的真理");
				SearchDataAccess.InsertTopic("基要真理", "得救的确据,信心,悔改,信心和行为,读经,祷告,恩典,圣洁,受洗,灵命成熟,传福音");
				SearchDataAccess.InsertTopic("圣经", "每日灵修,解经,读经,查经,应用");
				SearchDataAccess.InsertTopic("罪", "罪的捆绑,向罪死,罪的欺骗性,罪的赦免,罪的惩罚,与神隔绝,罪的羞辱,罪的工价");
				SearchDataAccess.InsertTopic("基督徒的品格", "仁爱,喜乐,和平,忍耐,恩慈,良善,信实,温柔,节制");
				SearchDataAccess.InsertTopic("基督徒生命价值", "怜悯,勇气,忍耐,信心,饶恕,慷慨,圣洁,盼望,谦卑,公义,爱,顺服,清洁的心,委身,服事,智慧,敬虔,知足");
				SearchDataAccess.InsertTopic("教会", "基督的身体,新妇,基督为首,神的家,目的,主日崇拜,圣餐");
				SearchDataAccess.InsertTopic("门徒建造", "代价,果效,圣灵带领,世上的光,爱,坚韧不拔");
				dsTopics = SearchDataAccess.GetTopic();
			}

			this.ListBox_Title.DataSource = dsTopics.Tables[0].DefaultView;
			this.ListBox_Title.DataTextField = "Topic";
			this.ListBox_Title.DataValueField = "TopicID";
			this.ListBox_Title.DataBind();

			this.ListBox_Title.SelectedIndex = 0;
			this.TextBox_Title.Text = this.ListBox_Title.SelectedItem.Text;
		}

		private void BindListBoxSub()
		{
			DataSet ds = new DataSet();
			ds = SearchDataAccess.GetTopic();

			ds.Tables[0].DefaultView.RowFilter = "TopicID=" + this.ListBox_Title.SelectedValue + "";
			this.ListBox_SubTitle.Items.Clear();
			if(ds.Tables[0].DefaultView.Count > 0)
			{
				string[] arrsubtopics = ds.Tables[0].DefaultView[0].Row["subtopic"].ToString().Split(',');
				this.ListBox_SubTitle.DataSource = arrsubtopics;
				this.ListBox_SubTitle.DataBind();
				this.ListBox_SubTitle.SelectedIndex = 0;
			}
			
			if(this.ListBox_SubTitle.SelectedIndex > -1)
				this.TextBox_SubTitle.Text = this.ListBox_SubTitle.SelectedValue;
			else
				this.TextBox_SubTitle.Text = "";
		}

		protected void ListBox_Title_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.ListBox_Title.SelectedIndex > -1)
				this.TextBox_Title.Text = this.ListBox_Title.SelectedItem.Text;
			else
				this.TextBox_Title.Text = "";

			BindListBoxSub();

		}

		protected void ListBox_SubTitle_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.ListBox_SubTitle.SelectedIndex > -1)
				this.TextBox_SubTitle.Text = this.ListBox_SubTitle.SelectedValue;
			else
				this.TextBox_SubTitle.Text = "";
		}

		protected void Button_AddTitle_Click(object sender, System.EventArgs e)
		{
//			DataSet ds = new DataSet();
//			string filename = Server.MapPath(".") + "\\topics.xml";
//			ds.ReadXml(filename);
//
//			System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath(".") + "\\topics.xml", false);
//			string line = @"<?xml version=""1.0"" encoding=""utf-8"" ?>";
//			sw.WriteLine(line);
//			line = @"<NewDataSet>";
//			sw.WriteLine(line);
//			
//			string topic = "<topics>";
//			string topicend = "</topics>";
//			string mainname = "<mainname>";
//			string mainnameend = "</mainname>";
//			string subtopics = "<subtopics>";
//			string subtopicsend = "</subtopics>";
//
//			foreach(DataRow dr in ds.Tables["topics"].Rows)
//			{
//				sw.WriteLine(topic);
//
//				line = mainname + dr["mainname"].ToString() + mainnameend;
//				sw.WriteLine(line);
//				line = subtopics + dr["subtopics"].ToString() + subtopicsend;
//				sw.WriteLine(line);
//
//				sw.WriteLine(topicend);
//			}
//
//			sw.WriteLine(topic);
//			line = mainname + this.TextBox_Title.Text + mainnameend;
//			sw.WriteLine(line);
//			line = subtopics + "" + subtopicsend;
//			sw.WriteLine(line);
//			sw.WriteLine(topicend);
//
//
//			line = @"</NewDataSet>";
//			sw.WriteLine(line);
//
//			sw.Close();

			SearchDataAccess.InsertTopic(this.TextBox_Title.Text, "");

			this.BindListBox();
			this.BindListBoxSub();
		}

		protected void Button_UpdateTitle_Click(object sender, System.EventArgs e)
		{
			DataSet ds = SearchDataAccess.GetTopic();
			ds.Tables[0].DefaultView.RowFilter = "TopicID=" + this.ListBox_Title.SelectedValue;
			string subtopic = ds.Tables[0].DefaultView[0]["SubTopic"].ToString();
			SearchDataAccess.UpdateTopic(Convert.ToInt64(this.ListBox_Title.SelectedValue), this.TextBox_Title.Text, subtopic);

			this.BindListBox();
			this.BindListBoxSub();
		}

		protected void Button_DeleteTitle_Click(object sender, System.EventArgs e)
		{
			SearchDataAccess.DeleteTopic(this.ListBox_Title.SelectedValue);
			this.BindListBox();
			this.BindListBoxSub();
		}

		protected void Button_AddSubTitle_Click(object sender, System.EventArgs e)
		{

			DataSet ds = SearchDataAccess.GetTopic();
			ds.Tables[0].DefaultView.RowFilter = "TopicID=" + this.ListBox_Title.SelectedValue;
			string subtopic = ds.Tables[0].DefaultView[0]["SubTopic"].ToString();

			if(subtopic.Length > 0)
				subtopic += ",";
			subtopic += this.TextBox_SubTitle.Text.Trim();
			SearchDataAccess.UpdateTopic(Convert.ToInt64(this.ListBox_Title.SelectedValue), ds.Tables[0].DefaultView[0]["Topic"].ToString(), subtopic);

			this.BindListBoxSub();
		}

		protected void Button_UpdateSubTitle_Click(object sender, System.EventArgs e)
		{
			string subtopic = "";
			for(int i = 0; i < this.ListBox_SubTitle.Items.Count; i++)
			{
				if(this.ListBox_SubTitle.Items[i].Selected)
				{
					if(subtopic != "")
						subtopic += ",";
					subtopic += this.TextBox_SubTitle.Text.Trim();
				}
				else
				{
					if(subtopic != "")
						subtopic += ",";
					subtopic += this.ListBox_SubTitle.Items[i].Value;
				}
			}

			SearchDataAccess.UpdateTopic(Convert.ToInt64(this.ListBox_Title.SelectedValue), this.ListBox_Title.SelectedItem.Text, subtopic);

			this.BindListBoxSub();
		}

		protected void Button_DeleteSubTitle_Click(object sender, System.EventArgs e)
		{
			string subtopic = "";
			for(int i = 0; i < this.ListBox_SubTitle.Items.Count; i++)
			{
				if(!this.ListBox_SubTitle.Items[i].Selected)
				{
					if(subtopic != "")
						subtopic += ",";
					subtopic += this.ListBox_SubTitle.Items[i].Value;
				}
			}

			SearchDataAccess.UpdateTopic(Convert.ToInt64(this.ListBox_Title.SelectedValue), this.ListBox_Title.SelectedItem.Text, subtopic);

			this.BindListBoxSub();
		}




	}
}
